let enumIdList = new Array();

/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'upload', 'laydate', 'jquery'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let upload = layui.upload;
    let index;
    let timestamp = new Date().getTime();
    let copyTimetamp = timestamp;
    if (json.servProductPicture.productPictureId > 0) {
        reLoadImage("middlePictureOne", json.servProductPicture.middlePictureOne, "middlePictureOneValue");
        reLoadImage("middlePictureTwo", json.servProductPicture.middlePictureTwo, "middlePictureTwoValue");
        reLoadImage("middlePictureThree", json.servProductPicture.middlePictureThree, "middlePictureThreeValue");
        reLoadImage("middlePictureFour", json.servProductPicture.middlePictureFour, "middlePictureFourValue");
        reLoadImage("middlePictureFive", json.servProductPicture.middlePictureFive, "middlePictureFiveValue");

        reLoadImage("bigPictureOne", json.servProductPicture.bigPictureOne, "bigPictureOneValue");
        reLoadImage("bigPictureTwo", json.servProductPicture.bigPictureTwo, "bigPictureTwoValue");
        reLoadImage("bigPictureThree", json.servProductPicture.bigPictureThree, "bigPictureThreeValue");
        reLoadImage("bigPictureFour", json.servProductPicture.bigPictureFour, "bigPictureFourValue");
        reLoadImage("bigPictureFive", json.servProductPicture.bigPictureFive, "bigPictureFiveValue");
        reLoadImage("smallPicture", json.servProductPicture.smallPicture, "smallPictureValue");
        reLoadImage("microPicture", json.servProductPicture.microPicture, "microPictureValue");

        reLoadImage("showServiceProcessPictureOne", json.servProductPicture.showServiceProcessPictureOne, "showServiceProcessPictureOneValue");
        reLoadImage("showServiceProcessPictureTwo", json.servProductPicture.showServiceProcessPictureTwo, "showServiceProcessPictureTwoValue");
        reLoadImage("showServiceProcessPictureThree", json.servProductPicture.showServiceProcessPictureThree, "showServiceProcessPictureThreeValue");
        reLoadImage("showServiceProcessPictureFour", json.servProductPicture.showServiceProcessPictureFour, "showServiceProcessPictureFourValue");
        reLoadImage("showServiceProcessPictureFive", json.servProductPicture.showServiceProcessPictureFive, "showServiceProcessPictureFiveValue");
        reLoadImage("showServiceProcessPictureSix", json.servProductPicture.showServiceProcessPictureSix, "showServiceProcessPictureSixValue");
        if(json.servProductPicture.videoSrc){
            let next = $("#videoValue").next()
            let next1 = $(next).next();
            let next2 = $(next1).next();
            let next3 = $(next2).next();
            let next4 = $(next3).next();
            $(next).attr("style", "display:none !important");
            $(next1).attr("style", "display:none !important");
            $(next2).attr("style", "display:none !important");
            $(next3).attr("style", "display:none !important");
            $(next4).attr("style", "display:none !important");
        }
    }

    //初始化文件上传插件
    uploaderImage("middlePictureOne", "middlePictureOneValue");
    uploaderImage("middlePictureTwo", "middlePictureTwoValue");
    uploaderImage("middlePictureThree", "middlePictureThreeValue");
    uploaderImage("middlePictureFour", "middlePictureFourValue");
    uploaderImage("middlePictureFive", "middlePictureFiveValue");

    uploaderImage("bigPictureOne", "bigPictureOneValue");
    uploaderImage("bigPictureTwo", "bigPictureTwoValue");
    uploaderImage("bigPictureThree", "bigPictureThreeValue");
    uploaderImage("bigPictureFour", "bigPictureFourValue");
    uploaderImage("bigPictureFive", "bigPictureFiveValue");
    uploaderImage("smallPicture", "smallPictureValue");
    uploaderImage("microPicture", "microPictureValue");

    uploaderImage("showServiceProcessPictureOne", "showServiceProcessPictureOneValue");
    uploaderImage("showServiceProcessPictureTwo", "showServiceProcessPictureTwoValue");
    uploaderImage("showServiceProcessPictureThree", "showServiceProcessPictureThreeValue");
    uploaderImage("showServiceProcessPictureFour", "showServiceProcessPictureFourValue");
    uploaderImage("showServiceProcessPictureFive", "showServiceProcessPictureFiveValue");
    uploaderImage("showServiceProcessPictureSix", "showServiceProcessPictureSixValue");

    uploadeVideo("video", "videoValue");


    //  获取上一次上传图片的时间戳
    function getImageTimestamp(url) {
        var index = url.lastIndexOf("/");
        var str = url.substring(0, index);
        index = str.lastIndexOf("/");
        str = str.substring(index + 1, str.length);
        return str;
    }

    form.on('submit(zmn-form-submit)', function (obj) {
        for (let key in obj.field) {
            if (key.endsWith("Desc")) {
                if (obj.field[key] != null && obj.field[key] != undefined && obj.field[key] != "") {
                    if (!descVerify(obj.field[key])) {
                        return false;
                    }
                }
            } else if (key.endsWith("Sort")) {
                if (obj.field[key] != null && obj.field[key] != undefined && obj.field[key] != "") {
                    if (!sortValueVerify(obj.field[key])) {
                        return false;
                    }
                }
            }
        }
        let hasOne = $("input:hidden[name^='bigPicture'][value]").length > 0;
        if (!hasOne) {
            layer.msg("产品大图片至少上传一张");
            return false;
        }


        let data = obj.field;
        for (let k in data) {
            if (data[k] == '') {
                data[k] = null;
            }
        }
        let url = "/showProduct/picture/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
                });
            },
            success: function (json) {
                if (json.status == 200) {
                    layer.msg(json.message);
                } else {
                    zmnButtonUtil.enableButton("submit", $);
                }
                layer.msg(json.message);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        layer.close(index);
        return false;
    });

    function sortValueVerify(value) {
        if (!value.match('^[1-9]\\d*$')) {
            layer.alert("排序字段请输入正整数");
            return false;
        }

        if (value.length > 8) {
            layer.alert("排序字段长度不能超过8位");
            return false;
        }

        return true;
    }

    function descVerify(value) {
        if (value.length > 256) {
            layer.alert("图片描述长度超过256了，请调整后再试");
            return false;
        }

        return true;
    }

    function openVideo() {
        var classVideo = document.getElementById("videoValue").value;
        var index = layer.open({
            type: 2,
            content: '/uploadFiles/goLook?classVideo=' + classVideo,
            area: ['600px', '450px'],
            offset: 't',
            maxmin: true,
            end: function () {
            }
        });
    }

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId) {
        upload.render({
            elem: '#' + elemId
            , exts: 'jpg|png|jpeg|gif'
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/showProduct/picture/uploaderImage.action?timestamp=' + timestamp + '&elemId=' + elemId
            , size: 3072
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    let next2 = $(next1).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                    $(next2).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function uploadeVideo(elemId, valueId) {
        upload.render({
            elem: '#' + elemId
            , exts: 'avi|mp4|rmvb|wmv|mpg|mpeg'
            , accept: 'video'
            , data: {folderName: 'video/product/' + json.servProductVO.productId + '/productDetail', fileName: 'productVideo'}
            , url: '/common/uploadFile.action'
            , size: 10*1024
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#videoDisplay").attr("src",data.src);
                    let next = $("#videoValue").next()
                    let next1 = $(next).next();
                    let next2 = $(next1).next();
                    let next3 = $(next2).next();
                    let next4 = $(next3).next();
                    $(next).attr("style", "display:none !important");
                    $(next1).attr("style", "display:none !important");
                    $(next2).attr("style", "display:none !important");
                    $(next3).attr("style", "display:none !important");
                    $(next4).attr("style", "display:none !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }


    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //对外暴露的接口
    exports('product_picture_show_edit_js', {});
});

function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = document.getElementById(element).value;
    // img.src = obj.src;
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px', 300 + 'px'],
        shadeClose: true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}

function deleteImg(element) {
    var elementValue = element + 'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function (item) {
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}

function deleteVideo(element) {
    document.getElementById(element).src = "";
    let next = layui.$("#videoValue").next()
    let next1 = layui.$(next).next();
    let next2 = layui.$(next1).next();
    let next3 = layui.$(next2).next();
    let next4 = layui.$(next3).next();
    layui.$(next).attr("style", "display:block !important");
    layui.$(next1).attr("style", "display:block !important");
    layui.$(next2).attr("style", "display:block !important");
    layui.$(next3).attr("style", "display:block !important");
    layui.$(next4).attr("style", "display:block !important");
}

